/**
 * 
 */
package com.edocbase.desktop.plugins.mdairy.part.diary;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.draw2d.ActionEvent;
import org.eclipse.draw2d.ActionListener;

import com.edocbase.desktop.lightweight.widgets.RibbonButton;
import com.edocbase.desktop.lightweight.widgets.richeditor.commands.DocumentCommand;
import com.edocbase.desktop.lightweight.widgets.richeditor.commands.CommandInvoker;
import com.edocbase.desktop.lightweight.widgets.richeditor.commands.CommandState;

/**
 * @author lifei114@gmail.com
 * 
 */
public class ActionCommandWrapper implements ActionListener,
		PropertyChangeListener {
	private DocumentCommand cmd = null;
	private CommandInvoker editor = null;
	private RibbonButton button = null;

	public ActionCommandWrapper(CommandInvoker editor, DocumentCommand cmd,
			RibbonButton button) {
		super();
		this.editor = editor;
		this.cmd = cmd;
		this.cmd.setInvoker(editor);
		this.cmd.addPropertyChangeListener(this);
		this.button = button;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.draw2d.ActionListener#actionPerformed(org.eclipse.draw2d.
	 * ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent event) {
		cmd.execute();
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if ("state".equals(evt.getPropertyName())) {
			CommandState state = (CommandState) evt.getNewValue();
			switch (state) {
			case TRUE:
				button.setSelected(true);
				break;
			case FALSE:
				button.setSelected(false);
			default:
				break;

			}
		}
	}

}
